package com.example.smartf.tool;

import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

@Component
public class PythonUtils {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private JavaMailSender mailSender;

    @Value("1033457564@qq.com")
    private String from;

    public void sendMail(String title,String content,String email){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setSubject(title);
        message.setTo(email);
        message.setText(content);
        mailSender.send(message);
    }

    public void pythonExec(String username,String deviceID, String[] days){
        try{
            String exe = "python";
            String command = "/root/wordspace/train.py";
            String[] tmp = new String[] {exe,command,deviceID};
            String[] cmdArr = (String[]) ArrayUtils.addAll(tmp,days);

            logger.info("python 执行：" + exe + " " + command + " " + deviceID + Arrays.toString(days));

            Process process = Runtime.getRuntime().exec(cmdArr);

            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ( ( line = in.readLine() ) != null ){
                logger.info("python机器学习日志输出 ====" + line);
            }
            in.close();
            int result = process.waitFor();
            if( 0 == result ){
                //python 程序正常执行
                logger.info("python 机器学习 正常运行结束");
                logger.info("设备ID：" + deviceID + " 种植模型模型："  + "新增");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        sendMail("智慧农业系统-种植模型", "训练模型成功，请于网页查询！", username);
                        logger.info("发送模型通知消息：" + username + " 发送成功！");
                    }
                }).start();
            }else if( 1 == result ){
                //python 程序报错
                logger.warn("python 机器学习 程序报错！");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        sendMail("智慧农业系统-种植模型", "训练模型失败！", username);
                        logger.info("发送模型通知消息：" + username + " 发送成功！");
                    }
                }).start();
            }else {
                //python 文件找不到
                logger.warn("python 机器学习 程序文件不存在！");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        sendMail("智慧农业系统-种植模型", "训练模型失败！", username);
                        logger.info("发送模型通知消息：" + username + " 发送成功！");
                    }
                }).start();
            }
        }catch (IOException | InterruptedException e){
            e.printStackTrace();
        }
    }
    public void pythonExecExcel(String filename){

    }
}
